#!/usr/bin/env python


import sys
from time import time
from numpy import zeros, matrix
from random import uniform


N = 100

A = zeros((N,N))
for a in range(N):
    for b in range(N):
        A[a][b] = uniform(-2,2)
A = matrix(A)

B = zeros((N,N))
for a in range(N):
    for b in range(N):
        B[a][b] = uniform(-2,2)
B = matrix(B)


BPM = float(sys.argv[1])
SXNTH = ((60*4)/BPM)/16


print "A sixteenth is %f ms." %(SXNTH*1000.0)


count = [-1,-1]
t0 = told = time()
t1 = t0 + SXNTH
treallold = time()
try:
    while True:

        ####  Code starts here ####
        C = A*B
        if count[0]==16:
            count[0] = 1
            count[1] += 1
        else:
            count[0] += 1
            count[1] += 1
        s = int(t1-told)
        ms = int( ((t1-told)-s)*1000.0 )
        us = (t1-told-s-ms/1000.0)*1000000.0
        if count[1]%16==0: print "%5i: %3i sec, %3i ms, %.2f microsec" %(count[1],s,ms,us)
        #### ... and ends here ####

        while time()-t0 <= SXNTH:
            t1 = time()
        told = t0
        t0 = time()
except KeyboardInterrupt:
    actl_time = t0 - treallold
    true_time = SXNTH*count[1]
    error = actl_time - true_time
    percent_error = 100*(error/true_time)
    print "Total error was %.4f ms" %(error*1000)
    print "   within %.4f sec gives a %.5f percent error." %( actl_time, percent_error )
